﻿using FANUC.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class TodayRobotOutputPerHourController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(TodayRobotOutputsPerHourBarChartDtoOutput))]
        public IHttpActionResult Get(int id)
        {
            #region Test
            if (Helper.IsTest)
            {
                TodayRobotOutputsPerHourBarChartDtoOutput testData = new TodayRobotOutputsPerHourBarChartDtoOutput();
                testData.Date = System.DateTime.Now;
                testData.SlicesInfo = new List<decimal>() { 0, 0, 0, 0, 0, 0, 0, 0, 200M, 300M, 200M, 250M, 300M, 200M, 100M, 150M, 200M, 230M, 280M, 310M, 190M, 0, 0, 0 };

                return Ok(testData);
            }
            #endregion
            DateTime today = System.DateTime.Now.Date;
            var robot = db.Robot.Where(a => a.ID == id).FirstOrDefault();

            var perHourData = (from w in db.Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                               group w by new { w.Start_Time.Hour }
                                   into grp
                               select new
                               {
                                   grp.Key.Hour,
                                   TheHourTotalOutputProductQuantity = grp.Count(),
                               }).OrderBy(o => o.Hour).ToList();

            switch (robot.Robot_Type.Type)
            {
                case "Spot Welding":
                    perHourData=(from w in db.Spot_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                     group w by new { w.Start_Time.Hour }
                   into grp
                     select new
                     {
                         grp.Key.Hour,
                         TheHourTotalOutputProductQuantity = grp.Count(),
                     }).OrderBy(o => o.Hour).ToList();
                    break;
                case "Seal":
                    perHourData = (from w in db.Seal_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                     group w by new { w.Start_Time.Hour }
                   into grp
                     select new
                     {
                         grp.Key.Hour,
                         TheHourTotalOutputProductQuantity = grp.Count(),
                     }).OrderBy(o => o.Hour).ToList();
                    break;
                case "Press":
                    perHourData = (from w in db.Press_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                     group w by new { w.Start_Time.Hour }
                   into grp
                     select new
                     {
                         grp.Key.Hour,
                         TheHourTotalOutputProductQuantity = grp.Count(),
                     }).OrderBy(o => o.Hour).ToList();
                    break;
                case "Polish":
                    perHourData = (from w in db.Polish_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                     group w by new { w.Start_Time.Hour }
                   into grp
                     select new
                     {
                         grp.Key.Hour,
                         TheHourTotalOutputProductQuantity = grp.Count(),
                     }).OrderBy(o => o.Hour).ToList();
                    break;
            }

            if (perHourData.Count() <= 0)
            {
                TodayRobotOutputsPerHourBarChartDtoOutput noData = new TodayRobotOutputsPerHourBarChartDtoOutput();
                noData.Date = System.DateTime.Now;
                noData.SlicesInfo = new List<decimal>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                return Ok(noData);
            }

            TodayRobotOutputsPerHourBarChartDtoOutput data = new TodayRobotOutputsPerHourBarChartDtoOutput();
            data.Date = System.DateTime.Now;
            data.SlicesInfo = new List<decimal>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            foreach (var item in perHourData)
            {
                data.SlicesInfo[item.Hour] = item.TheHourTotalOutputProductQuantity;
            }

            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        { return null; }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

    }
}
